/*******************************************************************************
 *
 *    Copyright (c) Baina Info Tech Co. Ltd
 *
 *    MP3MusicMate_v1.4.9
 *
 *    EasyBatteryRecommendUtil
 *    TODO File description or class description.
 *
 *    @author: zuochen
 *    @since:  2011-12-16
 *    @version: 1.0
 *
 ******************************************************************************/

package great.app.luck.easybatteryrecommend;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

/**
 * EasyBatteryRecommendUtil of MP3MusicMate_v1.4.9.
 * <p>
 * 使用时，请调用{@link #registerRecommendReceiver()} 和
 * {@link #unregisterRecommendReceiver()}
 * </p>
 * 
 * @author zuochen
 */

public class EasyBatteryRecommendUtil {
	private static EasyBatteryRecommendUtil mBatteryRecommendUtil;

	EasyBatteryRecommendConfig mBatteryRecommendConfig;

	private static final int APP_LAUNCH_RECOMMEND_TIMES = 3;
	private static final int MAX_RECOMMEND_TIME = 6;
	private Context mContext;

	public EasyBatteryRecommendUtil(Context context) {
		this.mContext = context;
		mBatteryRecommendConfig = new EasyBatteryRecommendConfig(mContext);
	}

	public static EasyBatteryRecommendUtil getInstatnce(Context context) {
		if (null == mBatteryRecommendUtil) {
			mBatteryRecommendUtil = new EasyBatteryRecommendUtil(context);
		}
		return mBatteryRecommendUtil;
	}

	/**
	 * 开始推荐
	 * 
	 * @param title显示的标题的内容
	 * @param mesasge现实的
	 *            信息内容
	 */
	public void recommendEasyBattery(String title, String mesasge,
			String recommendPackage, int picID) {
		mBatteryRecommendConfig.increaseAppRunTimes();
		mBatteryRecommendConfig.saveRecommendPackageName(recommendPackage);
		if (mBatteryRecommendConfig.isFirstTimRunAPP()) {
			mBatteryRecommendConfig.changeIsFirstTimeRunAppState();
			NotifycationHelper.showDailyWallpaperNotifycation(mContext, title,
					mesasge, picID);
			return;
		}
		if (mBatteryRecommendConfig.isNeedRecommend()) {
			if (mBatteryRecommendConfig.getAppRunTimes() == APP_LAUNCH_RECOMMEND_TIMES) {
				NotifycationHelper.showDailyWallpaperNotifycation(mContext,
						title, mesasge, picID);
				return;
			}
		}
		return;

	}

	/**
	 * 开始推荐
	 * 
	 * @param title显示的标题的内容
	 * @param mesasge现实的
	 *            信息内容
	 */
	public void recommendByDialog(String title, String mesasge,
			String recommendPackage) {
		mBatteryRecommendConfig.increaseAppRunTimes();
		mBatteryRecommendConfig.saveRecommendPackageName(recommendPackage);
		if (mBatteryRecommendConfig.isFirstTimRunAPP()) {
			mBatteryRecommendConfig.changeIsFirstTimeRunAppState();
			showRecommendDialog(title, mesasge, recommendPackage);
			return;
		}
		if (mBatteryRecommendConfig.isNeedRecommend()) {
			if (mBatteryRecommendConfig.getAppRunTimes() == APP_LAUNCH_RECOMMEND_TIMES) {
				showRecommendDialog(title, mesasge, recommendPackage);
				return;
			}
		}
		return;

	}

	/**
	 * 开始推荐
	 * 
	 * @param title显示的标题的内容
	 * @param mesasge现实的
	 *            信息内容
	 */
	public void recommendByDialogAll(String title, String mesasge,
			String recommendPackage) {
		mBatteryRecommendConfig.increaseAppRunTimes();
		mBatteryRecommendConfig.saveRecommendPackageName(recommendPackage);
		if (mBatteryRecommendConfig.isFirstTimRunAPP()) {
			mBatteryRecommendConfig.changeIsFirstTimeRunAppState();
			showRecommendDialog(title, mesasge, recommendPackage);
			return;
		}
		if (mBatteryRecommendConfig.isNeedRecommend()) {
			showRecommendDialog(title, mesasge, recommendPackage);
			return;
		}
		return;
	}

	/**
	 * 开始推荐
	 * 
	 * @param title显示的标题的内容
	 * @param mesasge现实的
	 *            信息内容
	 */
	public void recommendByDialog2(String title, String mesasge,
			String recommendPackage) {
		mBatteryRecommendConfig.increaseAppRunTimes();
		mBatteryRecommendConfig.saveRecommendPackageName(recommendPackage);
		if (mBatteryRecommendConfig.isFirstTimRunAPP()) {
			mBatteryRecommendConfig.changeIsFirstTimeRunAppState();
			showRecommendDialog(title, mesasge, recommendPackage);
			return;
		}
		if (mBatteryRecommendConfig.isNeedRecommend()) {
			if (mBatteryRecommendConfig.getAppRunTimes() < MAX_RECOMMEND_TIME) {
				showRecommendDialog(title, mesasge, recommendPackage);
				return;
			}
		}
		return;

	}

	private static final long DELAY_TIME = 1000;
	private final Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
		}
	};

	private void showRecommendDialog(final String title, final String mesasge,
			final String recommendPackage) {
		// mHandler.postDelayed(new Runnable() {
		//
		// @Override
		// public void run() {
		new AlertDialog.Builder(mContext).setTitle(title).setMessage(mesasge)
				.setPositiveButton("Go To Market", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						mBatteryRecommendConfig.changeNeedRecommendState();
						gotoRating();
					}
				}).show();
		// }
		// }, DELAY_TIME);

	}

	private void gotoRating() {
		mBatteryRecommendConfig.changeNeedRecommendState();
		doRating(mContext, mBatteryRecommendConfig.getRecommendPakcageName());
	}

	private void doRating(Context context, String packageName) {
		final Uri marketUri = Uri.parse("market://details?id=" + packageName);
		final Intent intent = new Intent(Intent.ACTION_VIEW, marketUri);
		intent.addCategory(Intent.CATEGORY_BROWSABLE);
		try {
			context.startActivity(intent);
		} catch (Exception e) {
			Toast.makeText(context, "No Market Client", Toast.LENGTH_SHORT)
					.show();
		}

	}
}
